include Makefile.inc

CONFIGFILES = \
	kismet.conf \
	kismet_httpd.conf \
	kismet_alerts.conf \
	kismet_memory.conf \
	kismet_storage.conf \
	kismet_logging.conf \
	kismet_uav.conf \
	kismet_80211.conf 

# Dot11 parsers (modeled on former Kaitai model)
DOT11_PARSERS = \
	dot11_parsers/dot11_ie.cc.o \
	dot11_parsers/dot11_ie_7_country.cc.o \
	dot11_parsers/dot11_ie_11_qbss.cc.o \
	dot11_parsers/dot11_ie_33_power.cc.o \
	dot11_parsers/dot11_ie_36_supported_channels.cc.o \
	dot11_parsers/dot11_ie_45_ht_cap.cc.o \
	dot11_parsers/dot11_ie_48_rsn.cc.o \
	dot11_parsers/dot11_ie_52_rmm_neighbor.cc.o \
	dot11_parsers/dot11_ie_54_mobility.cc.o \
	dot11_parsers/dot11_ie_55_fastbss.cc.o \
	dot11_parsers/dot11_ie_61_ht_op.cc.o \
	dot11_parsers/dot11_ie_70_rm_capabilities.cc.o \
	dot11_parsers/dot11_ie_133_cisco_ccx.cc.o \
 	dot11_parsers/dot11_ie_191_vht_cap.cc.o \
 	dot11_parsers/dot11_ie_192_vht_op.cc.o \
	dot11_parsers/dot11_ie_127_extended_capabilities.cc.o \
 	dot11_parsers/dot11_ie_221_vendor.cc.o \
 	dot11_parsers/dot11_ie_221_ms_wps.cc.o \
 	dot11_parsers/dot11_ie_221_ms_wmm.cc.o \
 	dot11_parsers/dot11_ie_221_dji_droneid.cc.o \
 	dot11_parsers/dot11_ie_221_wfa_wpa.cc.o \
	dot11_parsers/dot11_action.cc.o \
	dot11_parsers/dot11_wpa_eap.cc.o

# To prevent a deps problem, we define these targets via autoconf to reference
# the variable setting the list
PROTOBUF_CPP_O_TARGET = @PROTOBUF_CPP_O_TARGET@
PROTOBUF_CPP_O = \
	protobuf_cpp/kismet.pb.cc.o \
	protobuf_cpp/http.pb.cc.o \
	protobuf_cpp/datasource.pb.cc.o \
	protobuf_cpp/linuxbluetooth.pb.cc.o \
	protobuf_cpp/sdrrtl433.pb.cc.o

PROTOBUF_CPP_H_TARGET = @PROTOBUF_CPP_H_TARGET@
PROTOBUF_CPP_H = \
	protobuf_cpp/kismet.pb.h \
	protobuf_cpp/http.pb.h \
	protobuf_cpp/datasource.pb.h \
	protobuf_cpp/linuxbluetooth.pb.h \
	protobuf_cpp/sdrrtl433.pb.h

PROTOBUF_C_O = \
	protobuf_c/kismet.pb-c.c.o \
	protobuf_c/datasource.pb-c.c.o \
	protobuf_c/linuxbluetooth.pb-c.c.o 

PROTOBUF_C_H = \
	protobuf_c/kismet.pb-c.h \
	protobuf_c/datasource.pb-c.h \
	protobuf_c/linuxbluetooth.pb-c.h


# Common pure-c code for capturesource binaries
DATASOURCE_COMMON_C_O = \
	$(PROTOBUF_C_O) \
	simple_ringbuf_c.c.o capture_framework.c.o 
DATASOURCE_COMMON_A = libkismetdatasource.a

CAPTURE_PCAPFILE_O = \
	capture_pcapfile.c.o
CAPTURE_PCAPFILE 	= kismet_cap_pcapfile
BUILD_CAPTURE_PCAPFILE = @BUILD_CAPTURE_PCAPFILE@

CAPTURE_LINUX_WIFI	= capture_linux_wifi/kismet_cap_linux_wifi
BUILD_CAPTURE_LINUX_WIFI = @BUILD_CAPTURE_LINUX_WIFI@

CAPTURE_HACKRF_SWEEP_O = \
	capture_hackrf_sweep.c.o
CAPTURE_HACKRF_SWEEP = kismet_cap_hackrf_sweep
BUILD_CAPTURE_HACKRF_SWEEP = @BUILD_CAPTURE_HACKRF_SWEEP@

CAPTURE_LINUX_BLUETOOTH = capture_linux_bluetooth/kismet_cap_linux_bluetooth
BUILD_CAPTURE_LINUX_BLUETOOTH = @BUILD_CAPTURE_LINUX_BLUETOOTH@

CAPTURE_OSX_COREWLAN = capture_osx_corewlan_wifi/kismet_cap_osx_corewlan_wifi
BUILD_CAPTURE_OSX_COREWLAN = @BUILD_CAPTURE_OSX_COREWLAN@

CAPTURE_SDR_RTL433 = capture_sdr_rtl433/kismet_cap_sdr_rtl433
BUILD_CAPTURE_SDR_RTL433 = @BUILD_CAPTURE_SDR_RTL433@

CAPTURE_FREAKLABS_ZIGBEE = capture_freaklabs_zigbee/kismet_cap_freaklabs_zigbee
BUILD_CAPTURE_FREAKLABS_ZIGBEE = @BUILD_CAPTURE_FREAKLABS_ZIGBEE@

CAPTURE_NRF_MOUSEJACK = capture_nrf_mousejack/kismet_cap_nrf_mousejack
BUILD_CAPTURE_NRF_MOUSEJACK = @BUILD_CAPTURE_NRF_MOUSEJACK@

# Do we build and install the python modules?
BUILD_PYTHON_MODULES = @BUILD_PYTHON_MODULES@

# Capture binaries to build
DATASOURCE_BINS = @DATASOURCE_BINS@

# Disabled; Kaitai generates unusable C++ code currently
# KAITAI_PARSERS = \
# 	kaitai_parsers/wpaeap.cc.o \
# 	kaitai_parsers/dot11_ie.cc.o \
# 	kaitai_parsers/dot11_action.cc.o \
# 	kaitai_parsers/dot11_ie_7_country.cc.o \
# 	kaitai_parsers/dot11_ie_11_qbss.cc.o \
# 	kaitai_parsers/dot11_ie_45_ht.cc.o \
# 	kaitai_parsers/dot11_ie_48_rsn.cc.o \
# 	kaitai_parsers/dot11_ie_48_rsn_partial.cc.o \
# 	kaitai_parsers/dot11_ie_52_rmm_neighbor.cc.o \
# 	kaitai_parsers/dot11_ie_54_mobility.cc.o \
# 	kaitai_parsers/dot11_ie_61_ht.cc.o \
# 	kaitai_parsers/dot11_ie_133_cisco_ccx.cc.o \
# 	kaitai_parsers/dot11_ie_191_vht_capabilities.cc.o \
# 	kaitai_parsers/dot11_ie_192_vht_operation.cc.o \
# 	kaitai_parsers/dot11_ie_221_vendor.cc.o \
# 	kaitai_parsers/dot11_ie_221_ms_wps.cc.o \
# 	kaitai_parsers/dot11_ie_221_ms_wmm.cc.o \
# 	kaitai_parsers/dot11_ie_221_dji_droneid.cc.o \
# 	kaitai_parsers/dot11_ie_221_wfa_wpa.cc.o

PSO	= util.cc.o macaddr.cc.o uuid.cc.o xxhash.cc.o boost_like_hash.cc.o sqlite3_cpp11.cc.o \
	globalregistry.cc.o \
	pollabletracker.cc.o ringbuf2.cc.o chainbuf.cc.o filewritebuf.cc.o buffer_handler.cc.o \
	packet.cc.o messagebus.cc.o configfile.cc.o getopt.cc.o \
	psutils.cc.o battery.cc.o \
	tcpserver2.cc.o tcpclient2.cc.o serialclient2.cc.o pipeclient.cc.o ipc_remote2.cc.o \
	$(PROTOBUF_CPP_O_TARGET) kis_external.cc.o \
	dlttracker.cc.o antennatracker.cc.o datasourcetracker.cc.o kis_datasource.cc.o \
	datasource_linux_bluetooth.cc.o datasource_rtl433.cc.o \
	kis_net_microhttpd.cc.o system_monitor.cc.o base64.cc.o \
	kis_httpd_websession.cc.o kis_httpd_registry.cc.o \
	gpstracker.cc.o kis_gps.cc.o gpsnmea.cc.o gpsserial2.cc.o gpstcp.cc.o \
	gpsgpsd2.cc.o gpsfake.cc.o gpsweb.cc.o \
	packetchain.cc.o \
	trackedelement.cc.o trackedcomponent.cc.o entrytracker.cc.o \
	tracked_location.cc.o devicetracker_component.cc.o \
	devicetracker_view.cc.o devicetracker_view_workers.cc.o \
	jsoncpp.cc.o json_adapter.cc.o \
	plugintracker.cc.o alertracker.cc.o timetracker.cc.o channeltracker2.cc.o \
	devicetracker.cc.o devicetracker_workers.cc.o devicetracker_httpd.cc.o \
	kis_dlt.cc.o kis_dlt_ppi.cc.o kis_dlt_radiotap.cc.o \
	kaitaistream.cc.o \
	$(DOT11_PARSERS) \
	phy_80211.cc.o phy_80211_dissectors.cc.o phy_rtl433.cc.o phy_zwave.cc.o \
	phy_bluetooth.cc.o phy_uav_drone.cc.o phy_nrf_mousejack.cc.o \
	dot11_fingerprint.cc.o kis_dissector_ipdata.cc.o \
	manuf.cc.o \
	logtracker.cc.o kis_ppilogfile.cc.o kis_databaselogfile.cc.o kis_pcapnglogfile.cc.o \
	messagebus_restclient.cc.o \
	streamtracker.cc.o \
	pcapng_stream_ringbuf.cc.o streambuf_stream_buffer.cc.o \
	devicetracker_httpd_pcap.cc.o phy_80211_httpd_pcap.cc.o \
	kis_database.cc.o storageloader.cc.o \
	kismet_server.cc.o 

PS	= kismet

ALL	= Makefile $(PS) $(DATASOURCE_BINS)

INSTBINS = $(PS) $(DATASOURCE_BINS)

all:	$(ALL)

all-with-plugins:
	@make plugins-clean
	@make all
	@make plugins

$(PS):	$(PROTOBUF_CPP_O_TARGET) $(PROTOBUF_CPP_H_TARGET) $(PSO) $(patsubst %c.o,%c.d,$(PSO))
	$(LD) $(LDFLAGS) -o $(PS) $(PSO) $(LIBS) $(CXXLIBS) $(PCAPLIBS) $(KSLIBS) -rdynamic

$(DATASOURCE_COMMON_A):	$(PROTOBUF_C_O) $(PROTOBUF_C_H) $(DATASOURCE_COMMON_C_O)
	$(AR) rcs $(DATASOURCE_COMMON_A) $(DATASOURCE_COMMON_C_O)

$(CAPTURE_PCAPFILE):	$(PROTOBUF_C_H) $(DATASOURCE_COMMON_A) $(CAPTURE_PCAPFILE_O)
	$(CC) $(LDFLAGS) -o $(CAPTURE_PCAPFILE) $(CAPTURE_PCAPFILE_O) $(DATASOURCE_COMMON_A) $(PCAPLIBS) $(DATASOURCE_LIBS)

$(CAPTURE_LINUX_WIFI):	$(PROTOBUF_C_H) $(DATASOURCE_COMMON_A) FORCE
	(cd capture_linux_wifi && $(MAKE))

$(CAPTURE_LINUX_BLUETOOTH):	$(PROTOBUF_C_H) $(DATASOURCE_COMMON_A) FORCE
	(cd capture_linux_bluetooth && $(MAKE))

$(CAPTURE_OSX_COREWLAN): $(PROTOBUF_C_H) $(DATASOURCE_COMMON_A) FORCE
	(cd capture_osx_corewlan_wifi && $(MAKE))

$(CAPTURE_SDR_RTL433): FORCE
	(cd capture_sdr_rtl433 && $(MAKE))

$(CAPTURE_HACKRF_SWEEP):	$(PROTOBUF_C_H) $(DATASOURCE_COMMON_A) $(CAPTURE_HACKRF_SWEEP_O)
	$(CC) $(LDFLAGS) -o $(CAPTURE_HACKRF_SWEEP) $(CAPTURE_HACKRF_SWEEP_O) $(DATASOURCE_COMMON_A) -lhackrf -lfftw3 $(LIBMLIB) -lpthread -lm

$(CAPTURE_NRF_MOUSEJACK): $(PROTOBUF_C_H) $(DATASOURCE_COMMON_A) FORCE
	(cd capture_nrf_mousejack && $(MAKE))

$(CAPTURE_FREAKLABS_ZIGBEE): FORCE
	(cd capture_freaklabs_zigbee && $(MAKE))

FORCE:

datasources:	$(DATASOURCE_BINS)

Makefile: Makefile.in configure
	@-echo "'Makefile.in' or 'configure' are more current than this Makefile.  You should re-run 'configure'."

binsuidinstall: $(DATASOURCE_BINS)
	mkdir -p $(BIN)

	@if test "$(BUILD_CAPTURE_LINUX_WIFI)"x = "1"x; then \
		$(INSTALL) -o $(INSTUSR) -g $(SUIDGROUP) -m 4550 $(CAPTURE_LINUX_WIFI) $(BIN)/`basename $(CAPTURE_LINUX_WIFI)`; \
	fi;

	@if test "$(BUILD_CAPTURE_LINUX_BLUETOOTH)"x = "1"x; then \
		$(INSTALL) -o $(INSTUSR) -g $(SUIDGROUP) -m 4550 $(CAPTURE_LINUX_BLUETOOTH) $(BIN)/`basename $(CAPTURE_LINUX_BLUETOOTH)`; \
	fi;

	@if test "$(BUILD_CAPTURE_OSX_COREWLAN)"x = "1"x; then \
		$(INSTALL) -o $(INSTUSR) -g $(SUIDGROUP) -m 4550 $(CAPTURE_OSX_COREWLAN) $(BIN)/`basename $(CAPTURE_OSX_COREWLAN)`; \
	fi;

	@if test "$(BUILD_CAPTURE_NRF_MOUSEJACK)"x = "1"x; then \
		$(INSTALL) -o $(INSTUSR) -g $(SUIDGROUP) -m 4550 $(CAPTURE_NRF_MOUSEJACK) $(BIN)/`basename $(CAPTURE_NRF_MOUSEJACK)`; \
	fi;

commoninstall: $(INSTBINS)
	mkdir -p $(ETC)
	mkdir -p $(BIN)

	$(INSTALL) -o $(INSTUSR) -g $(INSTGRP) -m 555 $(PS) $(BIN)/$(PS); 

	# Install the alias script
	$(INSTALL) -o $(INSTUSR) -g $(INSTGRP) -m 555 kismet_server $(BIN)/kismet_server; 

	mkdir -p $(BIN)

	@if test "$(BUILD_CAPTURE_PCAPFILE)"x = "1"x; then \
		$(INSTALL) -o $(INSTUSR) -g $(INSTGRP) $(CAPTURE_PCAPFILE) $(BIN)/$(CAPTURE_PCAPFILE); \
	fi;

	@if test "$(BUILD_CAPTURE_LINUX_WIFI)"x = "1"x; then \
		$(INSTALL) -o $(INSTUSR) -g $(INSTGRP) $(CAPTURE_LINUX_WIFI) $(BIN)/`basename $(CAPTURE_LINUX_WIFI)`; \
	fi;

	@if test "$(BUILD_CAPTURE_LINUX_BLUETOOTH)"x = "1"x; then \
		$(INSTALL) -o $(INSTUSR) -g $(INSTGRP) $(CAPTURE_LINUX_BLUETOOTH) $(BIN)/`basename $(CAPTURE_LINUX_BLUETOOTH)`; \
	fi;

	@if test "$(BUILD_CAPTURE_OSX_COREWLAN)"x = "1"x; then \
		$(INSTALL) -o $(INSTUSR) -g $(SUIDGROUP) $(CAPTURE_OSX_COREWLAN) $(BIN)/`basename $(CAPTURE_OSX_COREWLAN)`; \
	fi;

	@if test "$(BUILD_CAPTURE_HACKRF_SWEEP)"x = "1"x; then \
		$(INSTALL) -o $(INSTUSR) -g $(INSTGRP) $(CAPTURE_HACKRF_SWEEP) $(BIN)/$(CAPTURE_HACKRF_SWEEP); \
	fi;

	@if test "$(BUILD_CAPTURE_NRF_MOUSEJACK)"x = "1"x; then \
		$(INSTALL) -o $(INSTUSR) -g $(INSTGRP) $(CAPTURE_NRF_MOUSEJACK) $(BIN)/`basename $(CAPTURE_NRF_MOUSEJACK)`; \
	fi;

	@if test "$(BUILD_PYTHON_MODULES)"x = "1"x; then \
		(cd python_modules && $(MAKE) install) \
	fi;

	@if test "$(BUILD_CAPTURE_SDR_RTL433)"x = "1"x; then \
		(cd capture_sdr_rtl433 && $(MAKE) install) \
	fi;

	@if test "$(BUILD_CAPTURE_FREAKLABS_ZIGBEE)"x = "1"x; then \
		(cd capture_freaklabs_zigbee && $(MAKE) install) \
	fi;

	mkdir -p $(LIB)/pkgconfig
	$(INSTALL) -o $(INSTUSR) -g $(INSTGRP) -m 644 packaging/kismet.pc $(LIB)/pkgconfig/kismet.pc

	mkdir -p $(HTTPD)
	cp -r http_data/* $(HTTPD)

	cp conf/kismet_manuf.txt $(SHARE)/kismet_manuf.txt 


CONFINSTTARGETS = $(addprefix install_conf_, $(CONFIGFILES))
${CONFINSTTARGETS}: install_conf_%: 
	@if test -f $(ETC)/$*; then \
		echo "$(ETC)/$(f) already exists; it will not be automatically replaced."; \
		echo "HOWEVER if there have been any changes you will need to manually"; \
		echo "reconcile the differences in the config files!"; \
		echo "You can update all config files with"; \
		echo "make forceconfigs"; \
	else \
		$(INSTALL) -o $(INSTUSR) -g $(INSTGRP) -m 644 conf/$* $(ETC)/$*;  \
		echo install -o $(INSTUSR) -g $(INSTGRP) -m 644 conf/$* $(ETC)/$*;  \
	fi

FORCECONFINSTTARGETS = $(addprefix install_force_conf_, $(CONFIGFILES))
${FORCECONFINSTTARGETS}: install_force_conf_%: 
	$(INSTALL) -o $(INSTUSR) -g $(INSTGRP) -m 644 conf/$* $(ETC)/$*;  

suidinstall: $(PS) $(DATASOURCE_BINS)
	-groupadd -r -f $(SUIDGROUP)

	@$(MAKE) -e commoninstall
	@$(MAKE) -e binsuidinstall

	@$(MAKE) install_conf_kismet.conf
	@$(MAKE) install_conf_kismet_httpd.conf
	@$(MAKE) install_conf_kismet_memory.conf
	@$(MAKE) install_conf_kismet_alerts.conf
	@$(MAKE) install_conf_kismet_storage.conf
	@$(MAKE) install_conf_kismet_logging.conf
	@$(MAKE) install_conf_kismet_uav.conf
	@$(MAKE) install_conf_kismet_80211.conf

	@echo
	@echo
	@echo "Installed kismet into $(BIN)/."
	@echo "If you have not done so already, read the README file and the FAQ file.  Additional"
	@echo "documentation is in the docs/ directory.  You MUST edit $(ETC)/kismet.conf "
	@echo "and configure Kismet for your system, or it will NOT run properly!"
	@echo
	@echo "Kismet has been installed with a SUID ROOT CAPTURE HELPER executeable by "
	@echo "users in the group '" $(SUIDGROUP) "'.  This WILL ALLOW USERS IN THIS GROUP "
	@echo "TO ALTER YOUR NETWORK INTERACE STATES, but is more secure than running "
	@echo "all of Kismet as root.  ONLY users in this group will be able to "
	@echo "run Kismet and capture from physical network devices."
	@echo
	@echo "If you have just created this group, you will need to log out and back in"
	@echo "before your user will have access.   Check the output of the 'groups' "
	@echo "command to make sure your user has the proper group!"
	@echo
	@echo "If you have installed Kismet in the past, you may need to UPDATE YOUR CONFIG"
	@echo "FILES or Kismet may not work properly!  You can manually reconcile differences"
	@echo "or you can replace the previously installed config files entirely by running"
	@echo "make forceconfigs"

install: $(INSTBINS)
	@$(MAKE) -e commoninstall

	@$(MAKE) install_conf_kismet.conf
	@$(MAKE) install_conf_kismet_httpd.conf
	@$(MAKE) install_conf_kismet_memory.conf
	@$(MAKE) install_conf_kismet_alerts.conf
	@$(MAKE) install_conf_kismet_storage.conf
	@$(MAKE) install_conf_kismet_logging.conf
	@$(MAKE) install_conf_kismet_uav.conf
	@$(MAKE) install_conf_kismet_80211.conf

	@echo
	@echo
	@echo "Installed kismet into $(BIN)/."
	@echo "If you have not done so already, read the README file and the FAQ file.  Additional"
	@echo "documentation is in the docs/ directory.  You MUST edit $(ETC)/kismet.conf "
	@echo "and configure Kismet for your system, or it will NOT run properly!"
	@echo
	@echo "Kismet has NOT been installed suid-root.  This means you will need to start "
	@echo "it as root.  If you add your user to the $(SUIDGROUP) group and install "
	@echo "Kismet with 'make suidinstall', users in that group will be able to "
	@echo "run Kismet directly."
	@echo
	@echo "READ THE KISMET DOCUMENTATION ABOUT THE KISMET SECURITY MODEL TO"
	@echo "DECIDE IF YOU WANT TO INSTALL IT SUID-ROOT"
	@echo
	@echo "It is generally *MORE SECURE* to install Kismet with the suid-root "
	@echo "option."
	@echo
	@echo "If you have installed Kismet in the past, you may need to UPDATE YOUR CONFIG"
	@echo "FILES or Kismet may not work properly!  You can manually reconcile differences"
	@echo "or you can replace the previously installed config files entirely by running"
	@echo "make forceconfigs"

forceconfigs: 
	@$(MAKE) install_force_conf_kismet.conf
	@$(MAKE) install_force_conf_kismet_httpd.conf
	@$(MAKE) install_force_conf_kismet_memory.conf
	@$(MAKE) install_force_conf_kismet_alerts.conf
	@$(MAKE) install_force_conf_kismet_storage.conf
	@$(MAKE) install_force_conf_kismet_logging.conf
	@$(MAKE) install_force_conf_kismet_uav.conf
	@$(MAKE) install_force_conf_kismet_80211.conf

uavconfig:
	@echo "Generating kismet_uav.conf"
	@python tools/compile_uav_conf.py conf/kismet_uav.conf.yaml > conf/kismet_uav.conf

manuf:
	@echo "Generating kismet_manuf.txt"
	@python tools/create_oui_db.py > conf/kismet_manuf.txt

rpm:
	@echo "Disabling SUID installation (RPM will handle setting the SUID bit.)"
	@( export SUID="no"; export INSTGRP=`id -g`; export MANGRP=`id -g`; \
		export INSTUSR=`id -u`; $(MAKE) -e install )

depclean:
	@-rm -f *.d

clean:
	@-rm -f *.o *.mo
	@-rm -f *.d
	# @-rm -f kaitai_parsers/*.o
	# @-rm -f kaitai_parsers/*.d
	@-rm -f dot11_parsers/*.o
	@-rm -f dot11_parsers/*.d
	@-rm -f protobuf_cpp/*.pb.*
	@-rm -f protobuf_c/*.pb-c.*
	@-$(MAKE) all-plugins-clean
	@-rm -f $(PS)
	@-rm -f $(BUILD_CAPTURE_PCAPFILE)
	@-rm -f $(BUILD_CAPTURE_LINUX_WIFI)
	@-rm -f $(BUILD_CAPTURE_LINUX_BLUETOOTH)
	@-rm -f $(BUILD_CAPTURE_OSX_COREWLAN)
	@-rm -f $(BUILD_CAPTURE_HACKRF_SWEEP)
	@-rm -f $(DATASOURCE_COMMON_A)
	@(cd capture_linux_bluetooth && make clean)
	@(cd capture_linux_wifi && make clean)
	@(cd capture_osx_corewlan_wifi && make clean)
	@(cd capture_sdr_rtl433 && make clean)

distclean:
	@-$(MAKE) clean
	@-$(MAKE) all-plugins-clean
	@-rm -f *~
	@-rm -f config.status
	@-rm -f config.h
	@-rm -f config.log
	@-rm -rf packaging/ipkg/usr 
	@-rm -rf packaging/pak
	@-rm -rf *.ipk
	@-rm -f scripts/kismet
	@-rm -f Makefile

plugins: Makefile
	@( export KIS_SRC_DIR=`pwd`; for x in plugin-*/; do  if [ ! -d "$$x" ]; then continue; fi; echo "PLUGIN: $$x"; ( cd "$$x"; make; ); done )

restricted-plugins: Makefile
	@( export KIS_SRC_DIR=`pwd`; for x in restricted-plugin-*/; do  if [ ! -d "$$x" ]; then continue; fi; echo "PLUGIN: $$x"; ( cd "$$x"; make; ); done )

plugins-clean:
	@( export KIS_SRC_DIR=`pwd`; for x in plugin-*/; do  if [ ! -d "$$x" ]; then continue; fi; echo "PLUGIN-CLEAN: $$x"; ( cd "$$x"; make clean; ); done )

restricted-plugins-clean:
	@( export KIS_SRC_DIR=`pwd`; for x in restricted-plugin-*/; do  if [ ! -d "$$x" ]; then continue; fi; echo "PLUGIN-CLEAN: $$x"; ( cd "$$x"; make clean; ); done )

plugins-install: Makefile
	@( export KIS_SRC_DIR=`pwd`; for x in plugin-*/; do  if [ ! -d "$$x" ]; then continue; fi; echo "PLUGIN-INSTALL: $$x"; ( cd "$$x"; make install; ); done )

restricted-plugins-install: Makefile
	@( export KIS_SRC_DIR=`pwd`; for x in restricted-plugin-*/; do  if [ ! -d "$$x" ]; then continue; fi; echo "PLUGIN-INSTALL: $$x"; ( cd "$$x"; make install; ); done )

plugins-userinstall: Makefile
	@( export KIS_SRC_DIR=`pwd`; for x in plugin-*/; do  if [ ! -d "$$x" ]; then continue; fi; echo "PLUGIN-USERINSTALL: $$x"; ( cd "$$x"; make userinstall; ); done )

restricted-plugins-userinstall: Makefile
	@( export KIS_SRC_DIR=`pwd`; for x in restricted-plugin-*/; do  if [ ! -d "$$x" ]; then continue; fi; echo "PLUGIN-USERINSTALL: $$x"; ( cd "$$x"; make userinstall; ); done )

all-plugins: Makefile
	@$(MAKE) plugins
	@$(MAKE) restricted-plugins

all-plugins-install: Makefile
	@$(MAKE) plugins-install
	@$(MAKE) restricted-plugins-install

all-plugins-userinstall: Makefile
	@$(MAKE) plugins-userinstall
	@$(MAKE) restricted-plugins-userinstall

all-plugins-clean: Makefile
	@$(MAKE) plugins-clean
	@$(MAKE) restricted-plugins-clean

# Disabled
# kaitai-parsers:
# 	@echo "Re-generating the kaitai_struct parsers requires the gpl3 licensed "
# 	@echo "kaitai_struct compiler; see http://http://kaitai.io/ for more info!"
# 	kaitai-struct-compiler --outdir kaitai_parsers/ -t cpp_stl -I kaitai_definitions/ kaitai_definitions/*.ksy
# 	file-rename -v -f 's/\.cpp$$/\.cc/' kaitai_parsers/*.cpp 

protobuf_cpp/%.pb.cc.d: %.pb.cc 
protobuf_cpp/%.pb.cc.o: %.pb.cc
protobuf_cpp/%.pb.cc: protobuf_definitions/%.proto
	$(PROTOCBIN) --cpp_out=./protobuf_cpp/ -I protobuf_definitions/ protobuf_definitions/$*.proto
protobuf_cpp/%.pb.h: protobuf_definitions/%.proto
	$(PROTOCBIN) --cpp_out=./protobuf_cpp/ -I protobuf_definitions/ protobuf_definitions/$*.proto

protobuf_c/%.pb-c.c.o: %.pb-c.c 
protobuf_c/%.pb-c.c: protobuf_definitions/%.proto
	$(PROTOCCBIN) --c_out=./protobuf_c/ -I protobuf_definitions/ protobuf_definitions/$*.proto
protobuf_c/%.pb-c.h: protobuf_definitions/%.proto
	$(PROTOCCBIN) --c_out=./protobuf_c/ -I protobuf_definitions/ protobuf_definitions/$*.proto

%.c.o:	%.c
%.c.o : %.c %.c.d
	$(CC) $(CFLAGS) $(CPPFLAGS) -c $*.c -o $@

%.cc.o: %.cc
%.cc.o:	%.cc %.cc.d
	$(CXX) $(CXXFLAGS) $(CPPFLAGS) -c $*.cc -o $@

%.c.d:  %.c $(PROTOBUF_C_H)
	$(CC) -MM $(CFLAGS) $(CPPFLAGS) $*.c | sed -e "s/\.o/\.c.o/" > $*.c.d

%.cc.d: %.cc $(PROTOBUF_CPP_H_TARGET)
	$(CXX) -MM $(CXXFLAGS) $(CPPFLAGS) $*.cc | sed -e "s/\.o/\.cc.o/" > $*.cc.d

.PRECIOUS: %.c %.cc %.h %.Td %.c.d %.cc.d protobuf_cpp/%.pb.cc protobuf_cpp/%.pb.h protobuf_c/%.pb-c.c protouf_c/%.pb-c.h

include $(wildcard $(patsubst %cc.o,%cc.d,$(PSO)))
include $(wildcard $(patsubst %c.o,%c.d,$(PSO)))
include $(wildcard $(patsubst %c.o,%c.d,$(DATASOURCE_COMMON_C_O)))
ifneq ($(BUILD_CAPTURE_PCAPFILE)x, "x")
	include $(wildcard $(patsubst %c.o,%c.d,$(CAPTURE_PCAPFILE_O)))
endif

.SUFFIXES: .c .cc .o 

